﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsasrsNetOutPut
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/21 14:52:23
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunWMS.SpiService;

internal class BWcsAsrsCrnOutPut : IWcsAsrsCrnOutPut
{
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    public List<AsrsCrnOutPutBean> GetAsrsCrnOutPut()
    {
        var db = Db.Default;

        var wcsAsrsCrnInputs = db.WcsAsrsCrnOutputs.Where(t => t.UseFlag == 1).Select(t => t).OrderByDescending(t => t.Id).ToList();

        var areaIds = wcsAsrsCrnInputs.Select(t => t.AreaId).ToList();
        var crnIds = wcsAsrsCrnInputs.Select(t => t.CrnId).ToList();
        var equipIds = areaIds.Concat(crnIds).ToList();

        var outputLoc1s = wcsAsrsCrnInputs.Select(t => t.OutputLoc1).ToList();
        var outputLoc2s = wcsAsrsCrnInputs.Select(t => t.OutputLoc2).ToList();
        var inputIds = outputLoc1s.Concat(outputLoc2s).ToList();


        var equips = _RcsEquip.GetRcsEquipByIds(equipIds);
        var locs = _RcsLocation.GetLocationsByLocIds(inputIds);

        var result = wcsAsrsCrnInputs.Join(equips, f => f.AreaId, s => s.Id, (f, s) => new
        {
            Output = f,
            Area = s,
        }).Join(equips, f => f.Output.CrnId, s => s.Id, (f, s) => new
        {
            f.Output,
            f.Area,
            Crn = s,
        }).Join(locs, f => f.Output.OutputLoc1, s => s.Id, (f, s) => new
        {
            f.Output,
            f.Area,
            f.Crn,
            OutputLoc1 = s,
        }).Join(locs, f => f.Output.OutputLoc2, s => s.Id, (f, s) => new
        {
            f.Output,
            f.Area,
            f.Crn,
            f.OutputLoc1,
            OutputLoc2 = s,
        }).Select(t => new AsrsCrnOutPutBean
        {
            Id = t.Output.Id.GetValueOrDefault(),
            AreaName = t.Area.EquipName,
            CrnName = t.Crn.EquipName,
            CrnCode = t.Crn.Id,
            EquipEnable = t.Crn.EquipEnable,
            OutputLoc1 = t.OutputLoc1.LocName,
            OutputLoc2 = t.OutputLoc2.LocName,
            Number = t.Output.TaskCount.GetValueOrDefault(),
            Reason = t.Output.Reason
        }).ToList();

        return result;
    }
    public POWcsAsrsCrnOutput GetAsrsCrnOutPutByCrnId(long crnId)
    {
        var db = Db.Default;

        var wcsAsrsCrnInput = db.WcsAsrsCrnOutputs.Where(t => t.CrnId == crnId  && t.AreaId !=10020201).FirstOrDefault();
        return wcsAsrsCrnInput;
    }

    public int UpdateAsrsCrnOutPutCount(long id, int number, string reason)
    {
        var db = Db.Default;

        var result = db.WcsAsrsCrnOutputs.Where(t => t.Id == id).Update( new POWcsAsrsCrnOutput
        {
            TaskCount = number,
            UpdateTime = DateTime.Now,
            Reason = reason
        });

        return result;
    }
}
